;; https://projecteuler.net/problem=9

;; Special Pythagorean triplet

;; Problem 9

;; A Pythagorean triplet is a set of three natural numbers,
;; a < b < c, for which, a^2 + b^2 = c^2

;; For example, 32 + 42 = 9 + 16 = 25 = 52.

;; There exists exactly one Pythagorean triplet for which a
;; + b + c = 1000.

;; Find the product abc.


(import
  (except (rnrs base) let-values)
  (only (guile)
        lambda* λ
        simple-format
        current-output-port))


(define square
  (λ (n)
    (* n n)))


(define special-triplet?
  (λ (a b c)
    (= (+ (square a)
          (square b))
       (square c))))


(define find-triple-with-sum
  (λ (target-sum)
    (let loop-a ([a 1])
      (let loop-b ([b 1])
        (let loop-c ([c 1])
          (cond
           [(= a 1000) 'not-found]
           [(= b 1000) (loop-a (+ a 1))]
           [(= c 1000) (loop-b (+ b 1))]
           [(= (+ a b c) target-sum)
            (if (special-triplet? a b c)
                (list a b c)
                (loop-c (+ c 1)))]
           [else
            (loop-c (+ c 1))]))))))


(simple-format
 (current-output-port) "~a\n"
 (find-triple-with-sum 1000))  ; (200 375 425)
